package ltd.hxya.novel.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
//@ConditionalOnProperty(value = "spring.mail.enable")
public class MailUtils {

    @Value("${spring.mail.from}")
    private String from;

    @Value("${spring.mail.to}")
    private String to;


    @Autowired
    private JavaMailSender mailSender;

    public void sendMail(String subject,String text){

        mailSender.send(getSimpleMailMessage(subject,text));
    }

    public SimpleMailMessage getSimpleMailMessage(String subject,String text) {

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        simpleMailMessage.setSentDate(new Date());
        return simpleMailMessage;
    }
}
